#!/usr/bin/php -q
<?PHP
/* Copyright 2005-2018, Lime Technology
 * Copyright 2012-2018, Bergware International.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 2,
 * as published by the Free Software Foundation.
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 */
?>
<?
$docroot = $docroot ?? $_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp';
require_once "$docroot/webGui/include/Wrappers.php";
require_once "$docroot/plugins/dynamix.plugin.manager/include/PluginHelpers.php";

$var     = parse_ini_file('/var/local/emhttp/var.ini');
$current = parse_ini_file('/etc/unraid-version');
$unraid  = parse_plugin_cfg('dynamix', true);
$notify  = "$docroot/webGui/scripts/notify";
$server  = strtoupper($var['NAME']);
$output  = $unraid['notify']['plugin'];
$builtin = ['unRAIDServer'];

foreach ($builtin as $name) {
  $plg = "$name.plg";
  plugin('check',$plg);
  $file = "/tmp/plugins/$plg";
  $old = plugin('version', "/var/log/plugins/$plg");
  $new = plugin('version', $file);
  // silently suppress bad download of PLG file
  if (version_compare($new,$old,'>')) {
    exec("$notify -e ".escapeshellarg("System - $name [$new]")." -s ".escapeshellarg("停止 [$server] - 版本更新 $new")." -d ".escapeshellarg("有新版本的 $name 可用")." -i ".escapeshellarg("normal $output")." -x");
  }
}
exit(0);
?>
